//
//  AYuComposePictureView.swift
//  SHWeibo3
//
//  Created by 余伟康 on 16/7/27.
//  Copyright © 2016年 wangce. All rights reserved.
//

import UIKit
// cell可重用标识符
private let AYuComposePictureViewCellId = "AYuComposePictureViewCellId"
class AYuComposePictureView: UICollectionView {

    // 定义一个闭包通知Vc弹出相册
    var closure:(()->())?
    
    // 保存图片数组
    var imageList:[UIImage] = [UIImage]()
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        hidden = true
        let cellMargin: CGFloat = 5
        let cellWH = (SCREENW - 20 - cellMargin*2)/3
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置item
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = cellMargin
        layout.minimumLineSpacing = cellMargin
        // 设置数据源
        dataSource  = self
        delegate = self
        registerClass(AYuComposePictureViewCell.self, forCellWithReuseIdentifier: AYuComposePictureViewCellId)
    }
    
    // MARK: - 添加图片
    func addImages(image: UIImage){
        // 添加图片
        imageList.append(image)
        // 显示配图
        hidden = false
        // 刷新UI
        reloadData()
    }
}

extension AYuComposePictureView :UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        let count = imageList.count
        
        if count == 0 || count == 9{
            return count
        }
        
        // 其他
        return count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(AYuComposePictureViewCellId, forIndexPath: indexPath) as! AYuComposePictureViewCell
        cell.backgroundColor = RandomColor()
        
        if indexPath.item == imageList.count{
            cell.image = nil
        }else{
            cell.image = imageList[indexPath.item]
        }
        
        cell.closure = {[weak self] in
            // 移除指定位置的image
            self?.imageList.removeAtIndex(indexPath.item)
            
            if self?.imageList.count == 0{
                self?.hidden = true
            }
        
            // 刷新UI
            self?.reloadData()
        }
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        
        // 代表点击的加号按钮
        if indexPath.item == imageList.count{
            closure?()
        }
    }

}


// 自定义cell
class AYuComposePictureViewCell: UICollectionViewCell {
    
    // 定义一个闭包
    var closure:(()->())?
    // 定义一个属性供外界赋值
    var image: UIImage? {
        didSet{
            // 添加加号图片
            if image == nil {
                bgImageView.image = UIImage(named: "compose_pic_add")
                // 设置高亮图片
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                // 隐藏删除按钮
                deleteButton.hidden = true
            }else {
                bgImageView.image = image
                bgImageView.highlightedImage = nil
                // 显示删除按钮
                deleteButton.hidden = false
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 监听方法
    func buttonClick(){
        closure?()
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        // 添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        // 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    
    // MARK: - 懒加载控件
    // 配图
    private lazy var bgImageView: UIImageView = UIImageView()
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        // 添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        return button
    }()
    
}


















